
package prefuse.table;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.swing.*;

import prefuse.*;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataShapeAction;
import prefuse.action.layout.AxisLabelLayout;
import prefuse.action.layout.AxisLayout;
import prefuse.controls.ToolTipControl;
import prefuse.data.Table;
import prefuse.render.AbstractShapeRenderer;
import prefuse.render.AxisRenderer;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.Renderer;
import prefuse.render.RendererFactory;
import prefuse.render.ShapeRenderer;
import prefuse.util.ColorLib;
import prefuse.util.ui.UILib;
import prefuse.vensim.ReadText;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.VisiblePredicate;

public class TableCreaterbyDate {

	public static Table generateTable(String txtfile,String key,double[] timeList) throws IOException {
		System.out.println(timeList.length);
		String[] t = new String[timeList.length];
		
		for(int i=0;i<timeList.length;i++){
			double d = timeList[i];
			 if(d == (int) d)
				 	t[i]= String.format("%d",(int)d);
			    else
			    	t[i]= String.format("%s",d);
			
		}
		System.out.println("this is t.length: "+t.length);
		ReadText a = new ReadText(txtfile);
		a.searchMap(key,t);
		
		
		
		Table table = new Table();
		double[] dataText = new double[]{};
		dataText = a.getValue();
		
		
		int tableSize = timeList.length;
		
		// set up table schema
		table.addColumn("Date", double.class);
		table.addColumn("TempData", double.class);
		

		table.addRows(tableSize);
	
		for(int i=0; i<tableSize; i++){		
				table.set(i,0,timeList[i]);
				table.set(i,1,dataText[i]);		
		}
	

		return table;
	}
	private static String dayTimeToDate(int dayN) {
		int dayNumber = dayN - 1;
		Calendar origin = Calendar.getInstance();
		origin.set(1900, 00, 00); // init a origin calendar.
		origin.add(Calendar.DAY_OF_YEAR, dayNumber);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = format.format(origin.getTime());
		System.out.println("Today is:" + dateString);
		return dateString;

	}

	public static JComponent createVisualization(Table data) {
		Visualization vis = new Visualization();
		Display display = new Display(vis);

		// --------------------------------------------------------------------
		// STEP 1: setup the visualized data

		vis.add("data", data);

		// --------------------------------------------------------------------
		// STEP 2: set up renderers for the visual data
//
//		vis.setRendererFactory(new DefaultRendererFactory(
//						new ShapeRenderer(7)));//the size of the circle
		
		
		vis.setRendererFactory(new RendererFactory() {
			AbstractShapeRenderer sr = new ShapeRenderer(5);
//			Renderer arY = new AxisRenderer(Constants.FAR_LEFT,
//					Constants.CENTER);
			Renderer arX = new AxisRenderer(Constants.CENTER,
					Constants.FAR_BOTTOM);

			public Renderer getRenderer(VisualItem item) {
				return 
						 item.isInGroup("xlab") ? arX : sr;
			}
		});

		
		

		// --------------------------------------------------------------------
		// STEP 3: create actions to process the visual data

		AxisLayout x_axis = new AxisLayout("data", "Date", Constants.X_AXIS,
				VisiblePredicate.TRUE);

//		AxisLayout y_axis = new AxisLayout("data", "TempData", Constants.Y_AXIS,
//				VisiblePredicate.TRUE);
		
		AxisLabelLayout x_labels = new AxisLabelLayout("xlab", x_axis);//You can change the spacing the minimum spacing between axis labels
		//public AxisLabelLayout(String group, AxisLayout layout, Rectangle2D bounds, double spacing)

//		AxisLabelLayout y_labels = new AxisLabelLayout("ylab", y_axis);
		

		ColorAction color = new ColorAction("data", VisualItem.FILLCOLOR,
				ColorLib.rgb(100, 100, 255));


		ActionList draw = new ActionList();
		draw.add(x_axis);   //draw point
//		draw.add(y_axis);	//draw point
		draw.add(x_labels);
//		draw.add(y_labels);
		draw.add(color);
	//	draw.add(shape);
		draw.add(new RepaintAction());
		vis.putAction("draw", draw);

		// --------------------------------------------------------------------
		// STEP 4: set up a display and controls
		display.setHighQuality(true);
		display.setSize(500, 300);

		display.setBorder(BorderFactory.createEmptyBorder(15, 30, 15, 30));

		// --------------------------------------------------------------------
		// STEP 5: launching the visualization
		vis.run("draw");

		return display;
	}

	public static JFrame createAndShowGUI(JComponent display,String title) {
		final JFrame frame = new JFrame();

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle(title);
		frame.setLocation(700, 10);
		frame.getContentPane().add(display);
		frame.pack();
		frame.setVisible(true);
		
		return frame;
		

		 
		
		
	}
	private static final long serialVersionUID = 1L; 
	public static void main(String[] args) throws IOException {
		Locale.setDefault(Locale.US);
		UILib.setPlatformLookAndFeel();
		double[] timeList = new double[]{17.5,20,30};
		Table table = generateTable("C:\\tmp\\rabbitann.txt","deaths",timeList);
		final JComponent display = createVisualization(table);

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI(display,"ann");
			}
		});
	}

}
